/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UTIL;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tin Pham
 */
public class MysqlConnection {
    
    Connection connect = null;
    
    public MysqlConnection() {
        this.connect = null;
    }
    
    public boolean connect() {
        try {
            // Load the JDBC driver
            String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
            Class.forName(driverName);

            // Create a connection to the database
            String serverName = "localhost";
            String mydatabase = "picasa";
            String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
            String username = "root";
            String password = "";
            this.connect = DriverManager.getConnection(url, username, password);
            return true;
        } catch (ClassNotFoundException e) {
            // Could not find the database driver
        } catch (SQLException e) {
            // Could not connect to the database
        }
        return false;
    }
    
    public void close() {
        try {
            this.connect.close();
        } catch (SQLException ex) {
            Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Boolean executeCommand(String sql) {
        if (this.connect()) {
            try {
                Statement st = this.connect.createStatement();
                return st.execute(sql);
            } catch (SQLException ex) {
                Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
    
}
